home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / termsorc.lha / Extras / Source / gtlayout-source.lha / LTP_CreateMenuTemplate.c < prev    next >
C/C++ Source or Header  |  1995-09-24  |  5KB  |  243 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1995 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. **
  6. **  :ts=4
  7. */
  8.  
  9. #include "gtlayout_global.h"
  10.  
  11. #ifdef DO_MENUS
  12.  
  13.     /* LTP_CreateMenuTemplate(RootMenu *Root,LONG *Error,struct NewMenu *MenuTemplate):
  14.      *
  15.      *    Create the menu strip data.
  16.      */
  17.  
  18. BOOLEAN __regargs
  19. LTP_CreateMenuTemplate(RootMenu *Root,LONG *Error,struct NewMenu *MenuTemplate)
  20. {
  21.     MenuNode    *LastMenu    = NULL;
  22.     ItemNode    *LastItem    = NULL;
  23.     ItemNode    *LastSub    = NULL;
  24.  
  25.     MenuNode    *Menu;
  26.     ItemNode    *Item;
  27.     ItemNode    *Sub;
  28.  
  29.     BOOLEAN         Done;
  30.  
  31.     *Error = 0;
  32.  
  33.     Done = FALSE;
  34.  
  35.     do
  36.     {
  37.             // Check the type
  38.  
  39.         switch(MenuTemplate -> nm_Type)
  40.         {
  41.                 // Stop here?
  42.  
  43.             case NM_END:
  44.  
  45.                 DB(kprintf("NM_END\n"));
  46.  
  47.                     // Mark the last menu item
  48.  
  49.                 if(LastSub)
  50.                     LastSub -> Flags |= ITEMF_LastItem;
  51.                 else
  52.                 {
  53.                     if(LastItem)
  54.                         LastItem -> Flags |= ITEMF_LastItem;
  55.                 }
  56.  
  57.                 Done = TRUE;
  58.                 break;
  59.  
  60.                 // Found a new menu?
  61.  
  62.             case NM_TITLE:
  63.  
  64.                 DB(kprintf("NM_TITLE |%s|\n",MenuTemplate -> nm_Label));
  65.  
  66.                     // Stop on weird labels
  67.  
  68.                 if(MenuTemplate -> nm_Label == NM_BARLABEL)
  69.                     *Error = ERROR_OBJECT_WRONG_TYPE;
  70.                 else
  71.                 {
  72.                         // Choose the menu to use
  73.  
  74.                     if(!LastMenu)
  75.                         Menu = (MenuNode *)&Root -> Node;
  76.                     else
  77.                         Menu = NULL;
  78.  
  79.                     DB(kprintf("         make menu\n"));
  80.  
  81.                         // Make room for the menu
  82.  
  83.                     if(Menu = LTP_MakeMenu(Root,Menu,MenuTemplate))
  84.                     {
  85.                             // Link to last menu
  86.  
  87.                         if(LastMenu)
  88.                             LastMenu -> Menu . NextMenu = &Menu -> Menu;
  89.  
  90.                             // Mark the last menu item
  91.  
  92.                         if(LastSub)
  93.                             LastSub -> Flags |= ITEMF_LastItem;
  94.                         else
  95.                         {
  96.                             if(LastItem)
  97.                                 LastItem -> Flags |= ITEMF_LastItem;
  98.                         }
  99.  
  100.                         LastMenu    = Menu;
  101.                         LastItem    = NULL;
  102.                         LastSub        = NULL;
  103.  
  104.                             // Add the menu to the list
  105.  
  106.                         AddTail((struct List *)&Root -> MenuList,(struct Node *)Menu);
  107.                     }
  108.                     else
  109.                         *Error = ERROR_NO_FREE_STORE;
  110.                 }
  111.  
  112.                 break;
  113.  
  114.                 // Found a new menu item?
  115.  
  116.             case NM_ITEM:
  117.  
  118.                 DB(kprintf("NM_ITEM |%s|\n",MenuTemplate -> nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate -> nm_Label));
  119.  
  120.                     // We need a menu to attach this to
  121.  
  122.                 if(!LastMenu)
  123.                     *Error = ERROR_INVALID_COMPONENT_NAME;
  124.                 else
  125.                 {
  126.                     DB(kprintf("        make item\n"));
  127.  
  128.                         // Make room for the item
  129.  
  130.                     if(Item = LTP_MakeItem(Root,MenuTemplate))
  131.                     {
  132.                             // Link it in
  133.  
  134.                         if(LastItem)
  135.                             LastItem -> Item . NextItem = &Item -> Item;
  136.                         else
  137.                             LastMenu -> Menu . FirstItem = &Item -> Item;
  138.  
  139.                         LastItem    = Item;
  140.                         LastSub        = NULL;
  141.  
  142.                             // Add it to the item list
  143.  
  144.                         AddTail((struct List *)&Root -> ItemList,(struct Node *)Item);
  145.                     }
  146.                     else
  147.                         *Error = ERROR_NO_FREE_STORE;
  148.                 }
  149.  
  150.                 break;
  151.  
  152.             case NM_SUB:
  153.  
  154.                 DB(kprintf("NM_SUB |%s|\n",MenuTemplate -> nm_Label == NM_BARLABEL ? (STRPTR)"«Bar»" : MenuTemplate -> nm_Label));
  155.  
  156.                     // We need a menu item to attach this to
  157.  
  158.                 if(!LastItem)
  159.                     *Error = ERROR_INVALID_COMPONENT_NAME;
  160.                 else
  161.                 {
  162.                     DB(kprintf("       make sub\n"));
  163.  
  164.                         // Make room for the item
  165.  
  166.                     if(Sub = LTP_MakeItem(Root,MenuTemplate))
  167.                     {
  168.                             // Make sure it stays a submenu item
  169.  
  170.                         Sub -> Flags |= ITEMF_IsSub;
  171.  
  172.                             // Link it in
  173.  
  174.                         if(LastSub)
  175.                             LastSub -> Item . NextItem = &Sub -> Item;
  176.                         else
  177.                         {
  178.                                 // We cannot attach submenu items to
  179.                                 // separator bars
  180.  
  181.                             if(LastItem -> Flags & ITEMF_IsBar)
  182.                                 *Error = ERROR_INVALID_COMPONENT_NAME;
  183.                             else
  184.                             {
  185.                                 DB(kprintf("-----------> |%s| has submenu items\n",((struct IntuiText *)LastItem -> Item . ItemFill) -> IText));
  186.  
  187.                                     // Link the submenu item in
  188.  
  189.                                 LastItem -> Item . SubItem = &Sub -> Item;
  190.  
  191.                                     // Zap the command sequence data
  192.  
  193.                                 LastItem -> Flags        = (LastItem -> Flags & ~ITEMF_Command) | ITEMF_HasSub;
  194.                                 LastItem -> ExtraLabel    = NULL;
  195.  
  196.                                 LastItem -> Item . Flags    &= ~COMMSEQ;
  197.                                 LastItem -> Item . Command     = 0;
  198.  
  199.                                     // This is the first submenu item for this menu item
  200.  
  201.                                 Sub -> Flags |= ITEMF_FirstSub;
  202.                             }
  203.                         }
  204.  
  205.                         LastSub = Sub;
  206.  
  207.                             // Add it to the list
  208.  
  209.                         AddTail((struct List *)&Root -> ItemList,(struct Node *)Sub);
  210.                     }
  211.                     else
  212.                         *Error = ERROR_NO_FREE_STORE;
  213.                 }
  214.  
  215.                 break;
  216.         }
  217.  
  218.             // Proceed to the next entry
  219.  
  220.         MenuTemplate++;
  221.     }
  222.     while(!Done && !(*Error));
  223.  
  224.         // Did we create anything at all?
  225.  
  226.     if(!(*Error))
  227.     {
  228.         if(!Root -> MenuList . mlh_Head -> mln_Succ)
  229.             *Error = ERROR_OBJECT_NOT_FOUND;
  230.         else
  231.             LTP_FixExtraLabel(Root,Error);
  232.     }
  233.  
  234.     DB(kprintf("through, error=%ld\n",*Error));
  235.  
  236.     if(*Error)
  237.         return(FALSE);
  238.     else
  239.         return(TRUE);
  240. }
  241.  
  242. #endif    /* DO_MENUS */
  243.